using NL.Web;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace NL.Advanced
{
    public class Commander : MonoBehaviour
    {

        /// <summary>
        /// 指令表
        /// </summary>
        public string Url_CommandFile = "http://192.168.11.54/got/gm/commander.info";
        public string Url_CommandFile_Local = "info/commander.info";

        public Button Button_SpeedUp, Button_SpeedDown;
        public Toggle LockFPS;

        public bool AutoHideCanvas = true;
        /// <summary>
        /// 指令面板
        /// </summary>
        public WinCommander Mod_WinCommander;

        WinCommander _WinCommander;

        //public UIInfoEditor MOD_UIInfoEditor;

        Canvas _Canvas;
        private void Awake()
        {
            _Canvas = GetComponent<Canvas>();
            if (_Canvas != null && AutoHideCanvas)
                _Canvas.enabled = false;

        }
        IEnumerator Start()
        {
            //if(Quality!=null)
            //{
            //    Quality.value = SETTING.Quality;
            //}
            //////
            ///  id name(呼叫的函数名) desc   type(SETTING) opt(参数)    text(附加文本)
            ///
            bool _outloaded = false;
            HTTP.Request<NLTable>(Url_CommandFile).Wait((w, t) =>
            {
                _outloaded = true;
                ALL = t;
            });
            while (!_outloaded)
            {
                yield return null;
            }
            if (ALL == null)
            {
                HTTP.Request<NLTable>(Url_CommandFile_Local).Wait((w, t) =>
                {
                    _outloaded = true;
                    ALL = t;
                });
            }
            if (Button_SpeedUp != null)
            {
                Button_SpeedUp.onClick.AddListener(() =>
                {
                    SETTING.SPEED_UP(0.25f);
                });
            }
            if (Button_SpeedDown != null)
            {
                Button_SpeedDown.onClick.AddListener(() =>
                {
                    SETTING.SPEED_UP(-0.25f);
                });
            }
            if (LockFPS != null)
            {
                LockFPS.onValueChanged.AddListener(b =>
                {
                    SETTING.FPSLock(b);
                });
            }
        }

        NLTable ALL;

        public void ShowWinCommander()
        {
            _ShowUIOrClose();
        }

        void _ShowUIOrClose()
        {
            if (_Canvas != null)
                _Canvas.enabled = true;


            if (Mod_WinCommander == null && _WinCommander == null) return;

            if (_WinCommander != null && _WinCommander.Showing)
            {
                _WinCommander.Close(); return;
            }



            _WinCommander = _WinCommander ?? Instantiate<WinCommander>(Mod_WinCommander, transform);


            _WinCommander?.Set(ALL).WaitSubmit((w) =>
            {

                _DoCommandSend(w.Info);

            }).Show();

            //_WinCommander.SetMSGDatas(OrderInfoManager.GETSumAll());

        }
        void _DoCommandSend(NLIni ini)
        {
            var all = GetComponentsInChildren<ICommandExcute>();
            foreach (var a in all)
                a.Excute(ini);

        }
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.BackQuote))
            {
                _ShowUIOrClose();

                return;
                //if (_ORDER_MNG == null)
                //{
                //    _ORDER_MNG = gameObject.ADDChild("[ORDER_MNG]").AddComponent<OrderInfoManager>()
                //        .WaitLoadDataCompleted(()=>
                //        {
                //            _ShowOrderPanel();
                //        });
                //    _ORDER_MNG.url = Url_CommandFile;
                //    _ORDER_MNG.RowStart = 1;
                //}
                //if (IsDebugPanelOpened)
                //{
                //    if (_WinCommander)
                //        _WinCommander.Close();

                //    UIInfoEditor editor = _ORDER_MNG.GetComponentInChildren<UIInfoEditor>();
                //    if (editor != null)
                //        editor.Close();
                //} 
                //else
                //{
                //    if (_WinCommander)
                //        _WinCommander.Show();
                //}
                //IsDebugPanelOpened = !IsDebugPanelOpened;
            }
        }

        //OrderInfoManager _ORDER_MNG;


        void _ShowOrderPanel()
        {
            //_WinCommander = _ORDER_MNG.GetComponent<WinCommander>();
            //if (_WinCommander == null)
            //{
            //    _WinCommander = GameObject.Instantiate<WinCommander>(Mod_WinCommander);
            //    _WinCommander.transform.SetParent(_ORDER_MNG.transform);

            //    _WinCommander.SetMSGDatas(OrderInfoManager.GETSumAll(), "proto_name")
            //        .WaitMSGChanged(info =>
            //        {
            //            _WinCommander.RefreshDropdownOpt(((IFOrderSummary)info).Children, "opt_desc");
            //        })
            //        .WaitOPTChanged(opt => 
            //        {
            //           LOG.Add($"[OrderPanel OPT->]:{opt}");
            //            UIInfoEditor editor = _ORDER_MNG.GetComponentInChildren<UIInfoEditor>();
            //            if (editor != null)
            //            {
            //                _ShowUIInfoEditor(_WinCommander.GetParamsInfo());
            //            }
            //        })
            //        .WaitCommand((w,c)=> 
            //        {
            //            switch (c.ToUpper())
            //            {
            //                case "SEND":
            //                    //SceneManager.LoadScene("sc_Launch");
            //                    string paramsInfo = _WinCommander.GetParamsInfo();
            //                    if (!string.IsNullOrEmpty(paramsInfo))
            //                    {
            //                        string prot = "proto_name=" + (w as WinCommander).OptIni["proto_name"].ToString() + ";" + paramsInfo;
            //                       LOG.Add($"[OrderPanel SEND Proto->]:{prot}");
            //                        NetGOT.CommandSend((ushort)(w as WinCommander).OptIni["proto_id"].AsInt, paramsInfo);



            //                        _DoCommandSend(paramsInfo);
            //                    }
            //                    break;
            //                case "EDIT":
            //                    _ShowUIInfoEditor(_WinCommander.GetParamsInfo());
            //                    break;
            //                default:
            //                    UI.ShowBox("WinHomeTopUI.Command", c.ToUpper());
            //                    break;
            //            }
            //        });
            //}
            //_WinCommander.Show();
        }

        void _ShowUIInfoEditor(string info)
        {
            //UIInfoEditor editor = _ORDER_MNG.GetComponentInChildren<UIInfoEditor>();
            //if (editor == null)
            //{
            //    editor = GameObject.Instantiate<UIInfoEditor>(MOD_UIInfoEditor);
            //    editor.transform.SetParent(_ORDER_MNG.transform);

            //    editor.WaitSubmit((w,s) => 
            //    {
            //        w.Close();
            //        _WinCommander.SetParamsInfo(s);
            //    });
            //}
            //editor.Set(info, ';').Reset().Show();
        }

        // bool IsDebugPanelOpened = false;






    }
}


